﻿module Sustainalytics.Accounts {
    export class AccountUsersService {
        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: INotificationFactory;
        private staticObjects: IStaticObjects;
        private $location: ng.ILocationService;

        constructor($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: INotificationFactory, staticObjects: IStaticObjects, $upload: any) {
            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.staticObjects = staticObjects;
            this.$location = $location;
        }

        private accountUsersUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/accountsserv/api/v1/accounts/";
        }

        private usersUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/accountsserv/api/v1/users/";
        }


        public accountUsersGetCount(accountId: string, status: string): ng.IPromise<number> {

            var ctx = this;

            var def = ctx.$q.defer();

            var promise = ctx.$http.get(ctx.accountUsersUrl() + accountId + '/users', { params: $.extend({ getcount: true }, { status: status, name: name }) });
            promise
                .success(data => { def.resolve(parseInt(<string>data)); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public accountUsersGetList(accountId: string, gridOpt: Common.HttpGetOptionsDto, status: string): ng.IPromise<UsersDto[]> {

            var ctx = this;

            var def = this.$q.defer();

            var promise = this.$http.get(ctx.accountUsersUrl() + accountId + '/users', { params: $.extend({ status: status }, gridOpt) });
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public accountUsersResetPasswords(items: string[]): ng.IPromise<boolean> {

            var ctx = this;
            var def = this.$q.defer();

            var newop = new AccountsPatchOpt();
            newop.resourceIds = items;
            newop.verb = "RESETPASSWORD";
            var ops: AccountsPatchOpt[] = [newop];

            var promise = ctx.$http({ method: "PATCH", url: ctx.usersUrl(), data: ops });
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }
      


        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjects.getMessageByInternalCode(err['code']));
        }

    }

    angular.module('Sustainalytics.Accounts')
        .factory('AccountUsersService',
        ($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: INotificationFactory, staticObjects: IStaticObjects, $upload: any) => {
            return new AccountUsersService($q, $http, $location, notificationFactory, staticObjects, $upload);
        }
        );
}





